class StoreController < ApplicationController
	before_filter :login_required
	layout "framework"
    def index
    	@session[:rights] = buildMenu '仓库管理'
    end
	def add_store
  		if !request.get?
                    #debugger
    		@store = Store.new(params[:store]) 
                store_name = Store.find_by_name(@params["store"]["name"])
                if store_name 
                    flash[:notice] = "<img src='/images/fail.gif'><font color='red'>仓库名重复！</font>"
                    redirect_to(:action=>'add_store')
                else
                    @store.state = 'OP'
                    if @store.in_type == 1
    			Store.update_all("in_type=0")
                    end
                    if @store.out_type == 1
    			Store.update_all("out_type=0")
                    end
                    if @store.save
    			flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{@store.name}已经成功增加！</font>"
    			redirect_to(:action=>'add_store')
                    end
                end
                end
  	end
  
  	def list_store
  		@stores = Store.find(:all)
  	end
  	
        def edit_store
            if request.get?
                @store = Store.find(@params["id"])
            else
                store_name = Store.find_by_name(@params["name"])
                if store_name && store_name.id != @params["id"].to_i
                    flash[:notice] = "<img src='/images/fail.gif'><font color='red'>仓库名重复！</font>"
                    redirect_to(:action=>'edit_store')
                else
                    store = Store.find(@params["id"])
                    store.name      = @params["name"]
                    store.address   = @params["address"]
                    store.phone     = @params["phone"]
                    if store.in_type != @params["in_type"].to_i
                        store.in_type  = @params["in_type"]
                        if store.in_type == 1
                            Store.update_all("in_type=0")
                        end
                    end
                    if store.out_type != @params["out_type"].to_i
                        store.out_type = @params["out_type"]
                        if store.out_type == 1
                            Store.update_all("out_type=0")
                        end
                    end
                    if store.save
                        flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{store.name}已经成功修改！</font>"
                        redirect_to(:action=>'list_store')
                    end
                end

            end
        end
  	
  	def stop_store
  		forward = nil
       store = Store.find(@params["id"])
       store.storeProducts.each do |storeProduct|
       	if storeProduct.current_count != 0
				forward = 'return'
        		break
        	end
        end
        if forward == 'return'
        	flash[:notice] = "<img src='/images/fail.gif'>&nbsp;<font color='red'>该仓库仍然拥有产品,在停用该仓库前请首先调配其中的产品！</font>"
        	redirect_to(:action=>'list_store')
        else
        	store.state = 'CL'
        	if store.save
        		flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{store.name}仓库已被停用！</font>"
    			redirect_to(:action=>'list_store')
        	end
        end
  	end

	def restart_store
       store = Store.find(@params["id"])
        	store.state = 'OP'
        	if store.save
        		flash[:notice] = "<img src='/images/ok.gif'><font color='green'>#{store.name}仓库已被启用!</font>"
    			redirect_to(:action=>'list_store')
        	end
  	end
end
